<template>
  <div class="divBox seckill">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="120px" inline>
            <el-form-item :label="$t('status1')" style="display: block">
              <el-radio-group
                v-model="tableFrom.product_status"
                type="button"
                class="mr20"
                size="small"
                clearable
                @change="getList('')"
              >
                <el-radio-button
                  v-for="(item, i) in fromList.fromTxt"
                  :key="i"
                  :label="item.val"
                  >{{ item.text }}</el-radio-button
                >
              </el-radio-group>
            </el-form-item>
            <el-form-item :label="$t('productsearch')">
              <el-input
                v-model="tableFrom.keyword"
                :placeholder="$t('Please5')"
                class="selWidth"
                @keyup.enter.native="getList(1)"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  class="el-button-solt"
                  @click="getList(1)"
                />
              </el-input>
            </el-form-item>
            <el-form-item :label="$t('boostactivitystatus')">
              <el-select
                v-model="tableFrom.type"
                :placeholder="$t('pleaseselect')"
                class="filter-item selWidth mr20"
                clearable
                @change="getList(1)"
              >
                <el-option
                  v-for="item in assistStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('activeproductstatus')">
              <el-select
                v-model="tableFrom.us_status"
                :placeholder="$t('pleaseselect')"
                class="filter-item selWidth mr20"
                clearable
                @change="getList"
              >
                <el-option
                  v-for="item in productStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('label')">
              <el-select
                v-model="tableFrom.mer_labels"
                :placeholder="$t('pleaseselect')"
                class="filter-item selWidth mr20"
                clearable
                filterable
                @change="getList(1)"
              >
                <el-option
                  v-for="item in labelList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <router-link :to="{ path: `${roterPre}` + '/marketing/assist/create' }">
          <el-button size="small" type="primary">
            <i class="add">+</i> {{ $t("addaboosterproduct") }}
          </el-button>
        </router-link>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
        :row-class-name="tableRowClassName"
        @rowclick.stop="closeEdit"
      >
        <el-table-column prop="product_assist_id" label="ID" min-width="50" />
        <el-table-column :label="$t('helpproductpictures')" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                :src="scope.row.product.image"
                :preview-src-list="[scope.row.product.image]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="store_name"
          :label="$t('tradename')"
          min-width="120"
        />
        <el-table-column :label="$t('boostprice')" min-width="90">
          <template slot-scope="scope">
            <span>{{
              scope.row.assistSku && scope.row.assistSku[0].assist_price
                ? scope.row.assistSku[0].assist_price
                : 0
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="stock"
          :label="$t('boostactivitystatus')"
          min-width="90"
        >
          <template slot-scope="scope">
            <span>{{
              scope.row.assist_status === 0
                ? $t("notstarted")
                : scope.row.assist_status === 1
                ? $t("underway")
                : $t("closed")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('activitytime')" min-width="160">
          <template slot-scope="scope">
            <div>
              {{ $t("startdate")
              }}{{
                scope.row.start_time && scope.row.start_time
                  ? scope.row.start_time.slice(0, 10)
                  : ""
              }}
            </div>
            <div>
              {{ $t("enddate")
              }}{{
                scope.row.end_time && scope.row.end_time
                  ? scope.row.end_time.slice(0, 10)
                  : ""
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('numberofsabc')"
          min-width="80"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.success }} / {{ scope.row.all }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('limit')" min-width="60">
          <template slot-scope="scope">
            <span>{{
              scope.row.assistSku && scope.row.assistSku[0]
                ? scope.row.assistSku[0].stock_count
                : 0
            }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('limitedsurplus')" min-width="60">
          <template slot-scope="scope">
            <span>{{
              scope.row.assistSku && scope.row.assistSku[0]
                ? scope.row.assistSku[0].stock
                : 0
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="product.sort"
          align="center"
          :label="$t('sort')"
          min-width="80"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.index === tabClickIndex">
              <el-input
                v-model.number="scope.row['product']['sort']"
                type="number"
                maxlength="300"
                size="mini"
                autofocus
                @blur="inputBlur(scope)"
              />
            </span>
            <span v-else @dblclick.stop="tabClick(scope.row)">{{
              scope.row["product"]["sort"]
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          :label="$t('upperowershelf')"
          min-width="80"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_show"
              :active-value="1"
              :inactive-value="0"
              :active-text="$t('putonshelves')"
              :inactive-text="$t('lowershelf')"
              @change="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="stock"
          :label="$t('productstatus')"
          min-width="90"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.us_status | productStatusFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="stock" :label="$t('label')" min-width="90">
          <template slot-scope="scope">
            <div
              v-for="(item, index) in scope.row.mer_labels"
              :key="index"
              class="label-list"
            >
              {{ item.name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('auditstatus')" min-width="130">
          <template slot-scope="scope">
            <span>{{
              scope.row.product_status === 0
                ? $t("tobereviewed")
                : scope.row.product_status === 1
                ? $t("approved")
                : $t("auditfailed")
            }}</span>
            <span
              v-if="scope.row.product_status === -1"
              style="font-size: 12px"
            >
              <br />
              {{ $t("reason") }}：{{ scope.row.refusal }}
            </span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('operation')" min-width="150" fixed="right">
          <template slot-scope="scope">
            <router-link
              v-if="scope.row.product_status === 0"
              :to="{
                path:
                  roterPre +
                  '/marketing/assist/create/' +
                  scope.row.product_assist_id
              }"
            >
              <el-button type="text" size="small" class="mr10">{{
                $t("edit")
              }}</el-button>
            </router-link>
            <el-button
              type="text"
              size="small"
              @click="handlePreview(scope.row.product_assist_id)"
              >{{ $t("preview") }}</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="onEditLabel(scope.row)"
              >{{ $t("editlabel") }}</el-button
            >
            <el-button
              type="text"
              size="small"
              class="mr10"
              @click="goDetail(scope.row.product_assist_id)"
              >{{ $t("details") }}</el-button
            >
            <el-button
              v-if="
                scope.row.product_status !== 1 || scope.row.assist_status == 2
              "
              type="text"
              size="small"
              class="mr10"
              @click="handleDelete(scope.row.product_assist_id, scope.$index)"
              >{{ $t("delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <el-dialog
      v-if="dialogVisible"
      :title="$t('helpproductdetails')"
      center
      :visible.sync="dialogVisible"
      width="700px"
    >
      <div v-loading="loading">
        <div class="box-container">
          <div class="title">{{ $t("basicinformation") }}：</div>
          <div class="acea-row">
            <div class="list sp">
              <label class="name">{{ $t("productid") }}：</label
              >{{ formValidate.product_id }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("tradename") }}：</label
              ><span>{{ formValidate.store_name }}</span>
            </div>
            <div class="list sp100 image">
              <label class="name">{{ $t("commoditymap") }}：</label>
              <img
                style="max-width: 150px; height: 80px"
                :src="formValidate.image"
              />
            </div>
          </div>
          <div class="title" style="margin-top: 20px">
            {{ $t("promotesttifno") }}：
          </div>
          <div class="acea-row">
            <div class="list sp100">
              <label class="name">{{ $t("introductiontotfhe") }}：</label
              >{{ formValidate.store_info }}
            </div>
            <div class="list sp100">
              <label class="name">{{ $t("dateofassistanceactivity") }}：</label
              >{{ formValidate.start_time + "-" + formValidate.end_time }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("boostprice") }}</label
              >{{ formValidate.price }}$
            </div>
            <div class="list sp">
              <label class="name">{{ $t("numberofgoodssold") }}：</label
              >{{ formValidate.pay }}{{ formValidate.unit_name }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("limit") }}：</label
              >{{ formValidate.stock_count }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("limitedsurplus") }}：</label
              >{{ formValidate.stock }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("numberofrestricteditems") }}：</label
              >{{ formValidate.pay_count }}{{ formValidate.unit_name }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("boosttimes") }}：</label
              >{{ formValidate.assist_user_count }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("numberofpeopleas") }}：</label
              >{{ formValidate.assist_count }}{{ $t("people") }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("auditstatus") }}：</label>
              <span>{{
                formValidate.reviewStatus === 0
                  ? $t("tobereviewed")
                  : formValidate.reviewStatus === 1
                  ? $t("approved")
                  : $t("auditfailed")
              }}</span>
              <span
                v-if="formValidate.reviewStatus === -1"
                style="font-size: 12px"
              >
                <br />
                {{ $t("reason") }}：{{ formValidate.refusal }}
              </span>
            </div>
            <div class="list sp">
              <label class="name">{{ $t("tips8") }}：</label
              >{{ formValidate.success }} / {{ formValidate.all }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("boostactivitystatus") }}：</label
              >{{
                formValidate.assist_status === 0
                  ? $t("notstarted")
                  : formValidate.assist_status === 1
                  ? $t("underway")
                  : $t("closed")
              }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("showstatus") }}</label
              >{{ formValidate.is_show === 1 ? $t("display") : $t("hide") }}
            </div>
            <div class="list sp">
              <label class="name">{{ $t("creationtime") }}：</label
              >{{ formValidate.create_time }}
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
    <!--预览商品-->
    <div v-if="previewVisible">
      <div class="bg" @click.stop="previewVisible = false" />
      <preview-box
        v-if="previewVisible"
        ref="previewBox"
        :goods-id="goodsId"
        :product-type="3"
        :preview-key="previewKey"
      />
    </div>
    <!--编辑标签-->
    <el-dialog
      v-if="dialogLabel"
      :title="$t('selectlabel')"
      :visible.sync="dialogLabel"
      width="800px"
      :before-close="handleClose"
    >
      <el-form ref="labelForm" :model="labelForm" @submit.native.prevent>
        <el-form-item>
          <el-select
            v-model="labelForm.mer_labels"
            clearable
            multiple
            :placeholder="$t('pleaseselect')"
            class="selWidth"
          >
            <el-option
              v-for="item in labelList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('labelForm')">{{
          $t("submit")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  assistProListApi,
  assistDeleteApi,
  assistStatusApi,
  assistProDetailApi,
  updatetAssistLabel,
  getProductLabelApi
} from "@/api/product";
import previewBox from "@/components/previewBox/index";
import { roterPre } from "@/settings";
import { assistProSort } from "@/api/marketing";
export default {
  name: "ProductList",
  components: { previewBox },
  data() {
    return {
      props: {
        emitPath: false
      },
      roterPre: roterPre,
      listLoading: true,
      tableData: {
        data: [],
        total: 0
      },
      assistStatusList: [
        { label: this.$t("notstarted"), value: 0 },
        { label: this.$t("underway"), value: 1 },
        { label: this.$t("closed"), value: 2 }
      ],
      productStatusList: [
        { label: this.$t("displayonshelf"), value: 1 },
        { label: this.$t("lowershelf"), value: 0 },
        { label: this.$t("platformclosing"), value: -1 }
      ],

      fromList: {
        custom: true,
        fromTxt: [
          { text: this.$t("all"), val: "" },
          { text: this.$t("tobereviewed"), val: "0" },
          { text: this.$t("reviewed"), val: "1" },
          { text: this.$t("auditfailed"), val: "-1" }
        ]
      },
      tableFrom: {
        page: 1,
        limit: 20,
        keyword: "",
        product_status: this.$route.query.status
          ? this.$route.query.status
          : "",
        type: "",
        us_status: "",
        mer_labels: "",
        product_assist_id: this.$route.query.id ? this.$route.query.id : ""
      },
      product_assist_id: this.$route.query.id ? this.$route.query.id : "",
      product_id: "",
      modals: false,
      dialogVisible: false,
      loading: false,
      manyTabTit: {},
      manyTabDate: {},
      formValidate: {},
      attrInfo: {},
      tabClickIndex: "",
      previewVisible: false,
      goodsId: "",
      previewKey: "",
      dialogLabel: false,
      labelList: [],
      labelForm: {}
    };
  },
  watch: {
    product_assist_id(newName, oldName) {
      this.getList("");
    }
  },
  mounted() {
    this.getList("");
    this.getLabelLst();
  },
  methods: {
    // 把每一行的索引放进row
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    // 添加明细原因 row 当前行 column 当前列
    tabClick(row) {
      this.tabClickIndex = row.index;
      //   this.tabClickLabel = column.label;
    },
    // 失去焦点初始化
    inputBlur(scope) {
      if (!scope.row.product.sort || scope.row.product.sort < 0)
        scope.row.product.sort = 0;
      assistProSort(scope.row.product_assist_id, {
        sort: scope.row.product.sort
      })
        .then(res => {
          this.closeEdit();
          //   this.$message.success(res.message);
        })
        .catch(res => {
          //  this.$message.error(res.message);
        });
    },
    closeEdit() {
      this.tabClickIndex = null;
    },
    // 表头换行
    renderheader(h, { column, $index }) {
      return h("span", {}, [
        h("span", {}, column.label.split("|")[0]),
        h("br"),
        h("span", {}, column.label.split("|")[1])
      ]);
    },
    // 获取标签项
    getLabelLst() {
      getProductLabelApi()
        .then(res => {
          this.labelList = res.data;
        })
        .catch(res => {
          this.$message.error(res.message);
        });
    },
    handleClose() {
      this.dialogLabel = false;
    },
    // 编辑标签
    onEditLabel(row) {
      this.dialogLabel = true;
      this.product_id = row.product_assist_id;
      if (row.mer_labels && row.mer_labels.length) {
        const arrNew = row.mer_labels.map(item => {
          return item.id;
        });
        this.labelForm = {
          mer_labels: arrNew
        };
      } else {
        this.labelForm = {
          mer_labels: []
        };
      }
    },
    submitForm(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          updatetAssistLabel(this.product_id, this.labelForm).then(
            ({ message }) => {
              this.$message.success(message);
              this.getList("");
              this.dialogLabel = false;
            }
          );
        } else {
          return;
        }
      });
    },
    watCh(val) {
      const tmp = {};
      const tmpTab = {};
      this.formValidate.attr.forEach((o, i) => {
        tmp["value" + i] = { title: o.value };
        tmpTab["value" + i] = "";
      });
      this.ManyAttrValue.forEach((val, index) => {
        const key = Object.values(val.detail)
          .sort()
          .join("/");
        if (this.attrInfo[key]) this.ManyAttrValue[index] = this.attrInfo[key];
      });
      this.attrInfo = {};
      this.ManyAttrValue.forEach(val => {
        this.attrInfo[
          Object.values(val.detail)
            .sort()
            .join("/")
        ] = val;
      });
      this.manyTabTit = tmp;
      this.manyTabDate = tmpTab;
      // this.formThead = Object.assign({}, this.formThead, tmp);
      // console.log(this.formThead)
    },
    // 查看详情
    goDetail(id) {
      this.dialogVisible = true;
      assistProDetailApi(id)
        .then(async res => {
          this.loading = false;
          const info = res.data;
          this.formValidate = {
            product_id: info.product_assist_id,
            image: info.product.image,
            store_name: info.store_name,
            store_info: info.store_info,
            start_time: info.start_time ? info.start_time : "",
            end_time: info.end_time ? info.end_time : "",
            create_time: info.create_time,
            unit_name: info.product.unit_name,
            is_show: info.is_show,
            stock_count: info.assistSku[0].stock_count,
            stock: info.assistSku[0].stock,
            content: info.content,
            price: info.assistSku[0].assist_price,
            assist_status: info.assist_status,
            reviewStatus: info.product_status,
            refusal: info.refusal,
            all: info.all,
            pay: info.pay,
            assist_user_count: info.assist_user_count,
            assist_count: info.assist_count,
            pay_count: info.pay_count,
            success: info.success
          };
          this.fullscreenLoading = false;
        })
        .catch(res => {
          this.fullscreenLoading = false;
          this.$message.error(res.message);
        });
    },
    // 预览
    handlePreview(id) {
      this.previewVisible = true;
      this.goodsId = id;
      this.previewKey = "";
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num || this.tableFrom.page;
      assistProListApi(this.tableFrom)
        .then(res => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        })
        .catch(res => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList("");
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList("");
    },
    // 删除
    handleDelete(id, idx) {
      this.$modalSure().then(() => {
        assistDeleteApi(id)
          .then(({ message }) => {
            this.$message.success(message);
            this.tableData.data.splice(idx, 1);
          })
          .catch(({ message }) => {
            this.$message.error(message);
          });
      });
    },
    onchangeIsShow(row) {
      assistStatusApi(row.product_assist_id, row.is_show)
        .then(({ message }) => {
          this.$message.success(message);
          this.getList("");
        })
        .catch(({ message }) => {
          this.$message.error(message);
        });
    }
  }
};
</script>

<style scoped lang="scss">
.bg {
  z-index: 100;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
}
::v-deep table .el-input__inner {
  padding: 0;
}
::v-deep .el-dialog__title {
  font-weight: bold;
}
.el-table .cell {
  white-space: pre-line;
}
.add {
  font-style: normal;
  position: relative;
  top: -1.2px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand1 {
  ::v-deep label {
    width: 77px !important;
    color: #99a9bf;
  }
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 33.33%;
}
.selWidth {
  width: 350px !important;
}
.seachTiele {
  line-height: 35px;
}
.title {
  margin-bottom: 16px;
  color: #17233d;
  font-size: 14px;
  font-weight: bold;
  padding-bottom: 2px;
  border-bottom: 1px solid #dfe6ec;
}
.scollhide::-webkit-scrollbar {
  display: none; /* Chrome Safari */
}
.box-container {
  overflow: hidden;
}
.box-container .list {
  float: left;
  line-height: 40px;
}
.box-container .sp {
  width: 50%;
}
.box-container .sp3 {
  width: 33.3333%;
}
.box-container .sp100 {
  width: 100%;
}
.box-container .list .name {
  display: inline-block;
  color: #606266;
}
.box-container .list .blue {
  color: #1890ff;
}
.box-container .list.image {
  margin: 20px 0;
  position: relative;
}
.box-container .list.image img {
  position: absolute;
  top: -20px;
}
.labeltop {
  height: 280px;
  overflow-y: auto;
}
</style>
